package com.add;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NewAutoPractice {
  private static final Log _log = LogFactory.getLog(NewAutoPractice.class);
  
  private static NewAutoPractice _instance;
  
  public static NewAutoPractice get() {
    if (_instance == null)
      _instance = new NewAutoPractice(); 
    return _instance;
  }
  
  public void load() {
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT * FROM `character_内挂仇人清单`");
      rs = pm.executeQuery();
      do {
      
      } while (rs.next());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
  }
  
  public void load3() {
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT * FROM `character_bad_buddy`");
      rs = pm.executeQuery();
      do {
      
      } while (rs.next());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
  }
  
  public void load2() {
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT * FROM `character_内挂被杀清单`");
      rs = pm.executeQuery();
      do {
      
      } while (rs.next());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
  }
  
  public void addEnemy(L1PcInstance pc) {
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT * FROM `character_内挂仇人清单`");
      rs = pm.executeQuery();
      while (rs.next()) {
        if (pc.getId() == rs.getInt("CharObjId"))
          pc.setInEnemyList(rs.getString("EnemyObjId")); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
  }
  
  public void AddEnemyList(L1PcInstance pc, String name) {
    int id = LoadMaxId() + 1;
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("INSERT INTO `character_内挂仇人清单` SET `Id`=?,`CharObjId`=?,`EnemyObjId`=?");
      pstm.setInt(1, id);
      pstm.setInt(2, pc.getId());
      pstm.setString(3, name);
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    pc.setInEnemyList(name);
  }
  
  public int LoadMaxId() {
    int id = 1;
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT MAX(Id) FROM character_内挂仇人清单");
      rs = pm.executeQuery();
      while (rs.next())
        id = rs.getInt(1); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
    return id;
  }
  
  public void DeleteEnemyList(L1PcInstance pc, String name) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `character_内挂仇人清单` WHERE `CharObjId`=? AND `EnemyObjId`=?");
      ps.setInt(1, pc.getId());
      ps.setString(2, name);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    pc.removeInEnemyList(name);
  }
  
  public void AddAutoList(L1PcInstance pc, L1Character srpc) {
    int id = LoadMaxId2() + 1;
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("INSERT INTO `character_内挂被杀清单` SET `Id`=?,`CharObjId`=?,`EnemyName`=?,`Time`=?");
      pstm.setInt(1, id);
      pstm.setInt(2, pc.getId());
      pstm.setString(3, srpc.getName());
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      String times = sdf.format(Long.valueOf(System.currentTimeMillis()));
      pstm.setString(4, times);
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public int LoadMaxId2() {
    int id = 1;
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      String sql = "SELECT MAX(Id) FROM character_内挂被杀清单";
      pm = co.prepareStatement("SELECT MAX(Id) FROM character_内挂被杀清单");
      rs = pm.executeQuery();
      while (rs.next())
        id = rs.getInt(1); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
    return id;
  }
  
  public void SearchAutoLog(L1PcInstance pc) {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstm = conn.prepareStatement("SELECT * FROM character_内挂被杀清单");
      rs = pstm.executeQuery();
      if (rs != null) {
        StringBuilder stringBuilder = new StringBuilder();
        int i = 0;
        while (rs.next()) {
          int objid = rs.getInt("CharObjId");
          String name = rs.getString("EnemyName");
          String time = rs.getString("Time");
          if (objid == pc.getId()) {
            stringBuilder.append(String.valueOf(String.valueOf(name)) + " [" + time + "],");
            i++;
          } 
        } 
        if (i == 0)
          stringBuilder.append("目前没有任何被杀相关纪录"); 
        String[] clientStrAry = stringBuilder.toString().split(",");
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "x_autolist", clientStrAry));
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(conn);
      SQLUtil.close(rs);
    } 
  }
  
  public void ClearAutoLog(int objid) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `character_内挂被杀清单` WHERE `CharObjId`=?");
      ps.setInt(1, objid);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public void addBadEnemy(L1PcInstance pc) {
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT * FROM `character_bad_buddy`");
      rs = pm.executeQuery();
      while (rs.next()) {
        if (pc.getId() == rs.getInt("CharObjId"))
          pc.setBadInEnemyList(rs.getString("EnemyObjId")); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
  }
  
  public void AddBadEnemyList(L1PcInstance pc, String name) {
    int id = LoadBadMaxId() + 1;
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("INSERT INTO `character_bad_buddy` SET `Id`=?,`CharObjId`=?,`EnemyObjId`=?");
      pstm.setInt(1, id);
      pstm.setInt(2, pc.getId());
      pstm.setString(3, name);
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    pc.setBadInEnemyList(name);
  }
  
  public int LoadBadMaxId() {
    int id = 1;
    Connection co = null;
    PreparedStatement pm = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement("SELECT MAX(Id) FROM character_bad_buddy");
      rs = pm.executeQuery();
      while (rs.next())
        id = rs.getInt(1); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
    return id;
  }
  
  public void DeleteBadEnemyList(L1PcInstance pc, String name) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `character_bad_buddy` WHERE `CharObjId`=? AND `EnemyObjId`=?");
      ps.setInt(1, pc.getId());
      ps.setString(2, name);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    pc.removeBadInEnemyList(name);
  }
}
